﻿﻿﻿﻿﻿﻿﻿﻿require.config({
  urlArgs: "version=20170407",
  paths: {
    'jquery': '/js/packages/jquery-2.14-min',
    'jquery_page':"/js/packages/jquery.twbsPagination",
    'bootstrap': '/bootstrap/js/bootstrap.min',
    'bootstrapdate': '/bootstrap/bootstrap-date/js/bootstrap-datetimepicker.min',
    
  },
  shim: {
    'bootstrap': {
      deps: ['jquery'],
      exports: 'bootstrap'
    },
  	'bootstrapdate':{
  		deps: ['jquery'],
        exports: 'bootstrap'
  	},
  	'jquery_page':{
  		deps: ['jquery'],
        exports: 'bootstrap'
  	}
  }
  
});

require(['jquery','bootstrap','bootstrapdate','jquery_page'], function ($) {
	$('#search-query').removeAttr("readonly");
	current_depot_id = 1;
	var curr_page = 1;
	var curr_size = 20;
	var search_dict = [];
	//----------------------------------------------
	var data_columns = ['id','content','createAt'];
	//var action_columns = ['重命名','删除'];
	set_url('/query/list/all');
	set_data_columns(data_columns);
	//set_action_columns(action_columns);
	set_has_input(true);
	var myfunction = function() {
		$('td>div>span').click(function(){
			//var id = $(this).parent().parent().find('>ul>li>label>input').prop('checked', true);
			if($(this).text()=="重命名") {
				rename(get_curr_span_id($(this)));
			}else if($(this).text()=="删除") {
				deletede(get_curr_span_id($(this)));
			}
		});
		$("#select-query-all").on("change",function(argument) {

			if($(this).prop("checked") ==true){
				$("#table-all>tr>td>input").prop('checked', true);
		     }else{
		    	 $("#table-all>tr>td>input").prop('checked', false);
		     }
		});
		$('#build-query').click(function(){
			searchQuery = makeQuery();
			$('#search-query-all').val(searchQuery);
		});
		function makeQuery() {
			searchQuery = '';
			search_dict = [];
			search_dict.push(0);
			$('tbody>tr>td>input').each(function(index){
				search_dict.push('( '+$(this).parent('td').parent('tr').find('>td.query-value>div')[0].innerHTML+' )');
				if($(this).prop("checked") ==true){
					if(searchQuery == '') {
						searchQuery = $(this).parent('td').parent('tr').find('>td.query-index')[0].innerHTML;
					}else {
						searchQuery = searchQuery + ' AND ' +$(this).parent('td').parent('tr').find('>td.query-index')[0].innerHTML
					}
				}
			});	
			return searchQuery;
		};
		function convertQuery(searchQuery) {     
			while(searchQuery.match(/#[0-9]+/g)!=null) {
				searchQuery = searchQuery.replace(/#[0-9]+/g,function(a){
				    //arguments中的每个元素对应一个参数
					return search_dict[parseInt(a.substring(1))];
				  });
			}
			return searchQuery;
		}
		$('#search-start').click(function(){
			search_wx();
		});

		function search_wx() {
			searchQuery = makeQuery();
			searchQuery = convertQuery(searchQuery);
			console.log(searchQuery);			
			$.ajax({
				url:'/search/wx',
			    type:'POST', //GET
			    async:true,    //或false,是否异步
			    data:{
			    	query:searchQuery
			    },
			    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
			    success:function(data,textStatus,jqXHR){
			    	window.location.href="/patents/patentslist.html";
			    	console.log(data)
			        console.log(textStatus)
			        console.log(jqXHR)
			    },
			    error:function(xhr,textStatus){
			        console.log('错误')
			        console.log(xhr)
			        console.log(textStatus)
			    },
			    complete:function(){
			        console.log('结束')
			    }
			});
		};
	};
	set_function(myfunction);
	init_page(curr_page,curr_size);
	
	
	
	

});
